home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / emul / cp4 / support / easy1541 / dev / examples / iecwrited64.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  4KB  |  251 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5.  
  6. #include <exec/exec.h>
  7. #include <exec/execbase.h>
  8. #include <exec/memory.h>
  9. #include <dos/dos.h>
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12.  
  13. #include <iec/iec.h>
  14.  
  15. //----------------------------------------
  16. //IECWriteD64 1.1.1
  17. //
  18. //Writes a D64 file to a disk inserted in
  19. //the 1541 drive.
  20. //
  21. //----------------------------------------
  22.  
  23.  
  24. struct iecbase *IECBase;
  25.  
  26. char Version[]="$VER:IECWriteD64 1.1.1 (15.10.96) by Fabrizio Farenga";
  27. UWORD BlockSize;
  28. char* DestName;    //Name of the D64 file to write (from AmigaDOS to the 1541)
  29.  
  30. FILE *fh;    //Input File Handle
  31.  
  32. struct RDArgs *rda;
  33. LONG argv[3]={0,0,0};
  34. char device=8;    //Default device #
  35.  
  36. //******************************************
  37. // Send a null-terminated string to the 1541
  38. //******************************************
  39. void SendString(char* string)
  40. {
  41. int t;
  42. for (t=0;string[t]!=0;t++) CIOut(string[t]); 
  43. }
  44.  
  45.  
  46.  
  47. //*****************************************************
  48. //Write sector MACRO
  49. //This write the TrackBuffer[] (256 bytes) to the sector
  50. //"s" of track "t"
  51. //*****************************************************
  52.  
  53. #define WRITESECTOR \
  54.     Listen(device); \
  55.     Second(CMD_DATA+2); \
  56.  \ 
  57.     c=0; \
  58.         while (c<256) \
  59.         { \
  60.         chkabort(); \
  61.         CIOut(TrackBuffer[c]); \
  62.         c++; \
  63.         } \
  64. \
  65.     UnListen(); \
  66. \
  67.     Listen(device);  \
  68.     Second(0x6f);  \
  69.     sprintf(command,"U2: 2 0 %d %d",t,s); \
  70.     SendString(command); \
  71. \
  72.     UnListen(); \
  73. \
  74. \
  75.     printf ("Writing Track: %d, Sector %d   \nF",t,s);
  76.  
  77. //*****************************************************
  78.  
  79. void WriteDisk(char* filename)
  80. {
  81. int t,s,c;
  82. int buffernum;
  83. char command[40];
  84. char TrackBuffer[256];
  85.  
  86. //Open the D64 file to write to the 1541
  87. fh=fopen (filename,"rb");
  88.  
  89.     if (fh==NULL)
  90.     {
  91.     printf ("\nFile Not Found\n\n");
  92.     return;
  93.     }
  94.  
  95. //Open the command channel
  96. Listen(device);    //OPEN 15,8,15
  97. Second(CMD_OPEN+15);
  98.  
  99.     if (IECBase->iec_ST!=ST_OK)
  100.     {
  101.     printf ("\n?DEVICE NOT PRESENT\n\n");
  102.     return;
  103.     }
  104.  
  105. //Reset the disk controller
  106. CIOut('I');        // PRINT#15,"I"
  107. UnListen();
  108.  
  109. //Open the data channel and allocate a buffer in the 1541 memory
  110. Listen(device);    //OPEN 2,8,2,"#"
  111. Second(CMD_OPEN+2);
  112. CIOut('#');
  113. UnListen();
  114.  
  115. //Get the buffer number (unused)
  116. Talk(device);    //GET #2,buffernum
  117. TkSA(CMD_DATA+2);
  118. buffernum=ACPtr();
  119. UnTalk();
  120.  
  121. //Reset to zero the buffer pointer
  122. Listen(device);    //PRINT #15,"B-P";2;0
  123. Second(CMD_DATA+15);
  124. SendString("B-P 2 0");
  125. UnListen();
  126.  
  127.  
  128.     //Write tracks 01 - 17 (21 sectors per track)
  129.     for (t=1;t<18;t++)
  130.     {
  131.         for (s=0;s<21;s++)
  132.         {
  133.         fread (TrackBuffer,256,1,fh);
  134.         WRITESECTOR
  135.         }
  136.     }
  137.  
  138.  
  139.     //Write tracks 18 - 24 (19 sectors per track)
  140.     for (t=18;t<25;t++)
  141.     {
  142.         for (s=0;s<19;s++)
  143.         {
  144.         fread (TrackBuffer,256,1,fh);
  145.         WRITESECTOR
  146.         }
  147.     }
  148.  
  149.     //Write tracks 25 - 31 (18 sectors per track)
  150.     for (t=25;t<31;t++)
  151.     {
  152.         for (s=0;s<18;s++)
  153.         {
  154.         fread (TrackBuffer,256,1,fh);
  155.         WRITESECTOR
  156.         }
  157.     }
  158.  
  159.     //Write tracks 31 - 35 (17 sectors per track)
  160.     for (t=31;t<36;t++)
  161.     {
  162.         for (s=0;s<17;s++)
  163.         {
  164.         fread (TrackBuffer,256,1,fh);
  165.         WRITESECTOR
  166.         }
  167.     }
  168.  
  169.  
  170. fclose (fh);
  171.  
  172.  
  173. Listen(device);    //Close 2
  174. Second(CMD_CLOSE+2);
  175. UnListen();
  176.  
  177. Listen(device);    //Close 15
  178. Second(CMD_CLOSE+15);
  179. UnListen();
  180.  
  181. printf ("\n\n");
  182.  
  183. }
  184.  
  185. /* Break Handler */
  186. int brk(void)
  187. {
  188. /* On break, close all */
  189.  
  190. fclose (fh);
  191.  
  192. Listen(device);    //Close 2
  193. Second(CMD_CLOSE+2);
  194. UnListen();
  195.  
  196. Listen(device);    //Close 15
  197. Second(CMD_CLOSE+15);
  198. UnListen();
  199.  
  200. CloseLibrary((struct Library*)IECBase);
  201. if (rda!=0) FreeArgs(rda);
  202.  
  203. printf ("\n\nBreak received.\n\n");
  204. return (1);
  205. }
  206.  
  207. void main(void)
  208. {
  209. IECBase = (struct iecbase*)OpenLibrary("iec.library",0L);
  210.  
  211. if ((rda = ReadArgs("FROM/A,DEVICE/N",argv,NULL)) != NULL)
  212.     {
  213.     if (argv[0]!=0) DestName=(char*)argv[0];    //Destination filename
  214.  
  215.     if (argv[1]!=0) device=*(LONG *)argv[1];    //Device (if none, device = 8)
  216.     }
  217. else
  218.     {
  219.     printf ("Required argument missing\n\n");
  220.     return;
  221.     }
  222.  
  223.  
  224. if (device<4)
  225.     {
  226.     printf ("\n?DEVICE NOT PRESENT\n\n");
  227.     return;
  228.     }
  229.  
  230.  
  231. if (IECBase!=0)
  232.     {
  233.  
  234.     /*Set the break handler*/
  235.     onbreak(&brk);
  236.  
  237.     WriteDisk(DestName);
  238.  
  239.     CloseLibrary((struct Library*)IECBase);
  240.  
  241.     }
  242. else
  243.     {
  244.     printf ("Can't open iec.library\n");
  245.     return;
  246.     }
  247.  
  248.     if (rda!=0) FreeArgs(rda);
  249.  
  250. }
  251.